package com.online_exam.controller.admin.question;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.online_exam.base.RestResponse;
import com.online_exam.domain.Question;
import com.online_exam.domain.TextContent;
import com.online_exam.domain.question.QuestionObject;
import com.online_exam.service.TextContentService;
import com.online_exam.service.UserService;
import com.online_exam.util.JwtUtil;
import com.online_exam.viewmodel.admin.question.QuestionEditVM;
import com.online_exam.service.QuestionService;
import com.online_exam.viewmodel.admin.question.QuestionPageResponseVM;
import com.online_exam.viewmodel.admin.question.QuestionPageRequestVM;
import com.online_exam.viewmodel.admin.question.QuestionResponseVM;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/admin/question")
public class QuestionController {

    private QuestionService questionService;
    private ModelMapper modelMapper;
    private TextContentService textContentService;
    private UserService userService;
    @Autowired
    public QuestionController(QuestionService questionService, ModelMapper modelMapper, TextContentService textContentService,UserService userService) {
        this.questionService = questionService;
        this.textContentService  = textContentService;
        this.modelMapper = modelMapper;
        this.userService = userService;
    }

    //需要修改下面下面id
    @PostMapping("/edit")
    public RestResponse editQuestion(@RequestBody QuestionEditVM questionEditVM, HttpServletRequest request){

        Integer id = JwtUtil.DecodeByToken(request,userService).getId();//表示获取用户ID，后面应该通过token拿到,这是添加题目的用户ID
        int index;
        if(questionEditVM.getId() == null){
            index = questionService.insertQuestionByVM(questionEditVM, id);

        }else{
            index = questionService.updateQuestionByEditVM(questionEditVM);

        }
        if (index>0){
            System.out.println("操作成功~~");
            return RestResponse.ok();
        }else{
            System.out.println("操作失败~~");
            return RestResponse.fail(101,"操作失败");
        }

    }

    @PostMapping("/page")
    public RestResponse<QuestionPageResponseVM> pagetQuestion(@RequestBody QuestionPageRequestVM vm){
        QuestionPageResponseVM questionPage = new QuestionPageResponseVM();
        Page<Object> page = PageHelper.startPage(vm.getPageIndex(), vm.getPageSize());
        List<Question> list = questionService.select(vm);
        questionPage.setPageNum(page.getPageNum());
        questionPage.setTotal(page.getTotal());
        System.out.println(list);
        List<QuestionResponseVM> collect = list.stream().map(
                (p) -> {
                    QuestionResponseVM questionResponseVM = modelMapper.map(p, QuestionResponseVM.class);
                    Integer id = questionResponseVM.getTextContentId();
                    TextContent textContent = textContentService.selectById(id);
                    QuestionObject questionObject = modelMapper.map(JSON.parse(textContent.getContent()),QuestionObject.class);
                    questionResponseVM.setShortTitle(questionObject.getTitleContent());
                    return questionResponseVM;
                }).collect(Collectors.toList());
        questionPage.setList(collect);
        System.out.println(questionPage);
        return RestResponse.ok(questionPage);
    }

    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<QuestionEditVM> select(@PathVariable Integer id) {
        QuestionEditVM newVM = questionService.getQuestionEditRequestVM(id);
        return RestResponse.ok(newVM);
    }

    @PostMapping(value = "/delete/{id}")
    public RestResponse delete(@PathVariable Integer id){
        int index = questionService.deleteQuestionById(id);
        if (index>0){
           return RestResponse.ok();
        }
        return RestResponse.fail(101,"删除不成功");
    }


}
